package vn.com.pavibook.admin.service;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import vn.com.pavibook.dao.AudioBookDao;
import vn.com.pavibook.dao.PublisherDao;
import vn.com.pavibook.domain.model.AudioBook;
import vn.com.pavibook.domain.model.PagingInfo;
import vn.com.pavibook.domain.model.SearchResult;
import vn.com.pavibook.model.vo.AudioBookVO;
import vn.com.pavibook.utils.DataHelper;

@Service
public class AudioBookServiceImpl implements AudioBookService {
	
	@Value("${url.audiobooks.server}")
	public String urlAudioServer;

	@Autowired
	private AudioBookDao audioBookDao;

	@Autowired
	private PublisherDao publisherDao;

	@Autowired
	private FileUploadService fileUploadService;
	
	@Override
	public boolean uploadAudio(AudioBookVO audioBookVO, String username,
			HttpServletRequest request) throws IllegalStateException, IOException {
		
		Date date = new Date();
		
		saveCreateDateAndPusherId(audioBookVO, username, date);
		
		boolean uploadImageStatus = uploadAudioImage(audioBookVO, username, date);
		
		boolean uploadAudioStatus = uploadAudio(audioBookVO, username, date);
		
		return uploadImageStatus && uploadAudioStatus;
	}


	private void saveCreateDateAndPusherId(AudioBookVO audioBookVO,
			String username, Date date) {
		int publisherId = publisherDao.getIdByUsername(username);
		audioBookVO.setPublisherId(publisherId);
		audioBookVO.setCreatedDate(date);
	}


	private boolean uploadAudio(AudioBookVO audioBookVO, String username,
			Date date) throws IllegalStateException, IOException {
		CommonsMultipartFile audioFile = audioBookVO.getAudioFileUpload();
		if (audioFile == null || audioFile.getSize() <= 0) {
			if(audioBookVO.getId() == null) {
				return false;
			}
			return true;
		}
		
		String urlAudio = "/" + username + "/audio_" + date.getTime() + "/file_" + date.getTime() +"/" + audioFile.getOriginalFilename();
		audioBookVO.setUrlAudio(urlAudio);
		String urlAudioFullPath = urlAudioServer + urlAudio;
		
		return fileUploadService.uploadFile(audioFile, urlAudioFullPath, 0);
	}


	private boolean uploadAudioImage(AudioBookVO audioBookVO, String username,
			Date date) throws IOException {
		CommonsMultipartFile imageAudioPartFile = audioBookVO.getImageFileUpload();
		if (imageAudioPartFile == null || imageAudioPartFile.getSize() <= 0) {
			return true;
		}
		
		String urlImageAudio = "/" + username + "/audio_" + date.getTime() + "/image_" + date.getTime() +"/" + imageAudioPartFile.getOriginalFilename();
		audioBookVO.setImageUrl(urlImageAudio);
		String urlImageAudioFullPath = urlAudioServer + urlImageAudio;
		
		return fileUploadService.uploadFile(imageAudioPartFile, urlImageAudioFullPath, Long.valueOf(DataHelper.ImageFile.IMAGE_FILE_MAX_LENGTH.getCode()));
	}


	@Override
	public void save(AudioBook audioBook) {
		audioBookDao.save(audioBook);
	}


	@Override
	public SearchResult<AudioBook> getAudioBookByPushlisher(String username, PagingInfo pagingInfo) {
		int publisherId = publisherDao.getIdByUsername(username);
		return audioBookDao.getAudioBook(publisherId, pagingInfo);
	}


	@Override
	public void deleteAudioBook(int id) {
		audioBookDao.delete(id);
	}


	@Override
	public SearchResult<AudioBook> getAudioBookById(int id) {
		return audioBookDao.getAudioBookByid(id);
	}


	@Override
	public SearchResult<AudioBook> getListAudioBookByUsername(String username,
			PagingInfo pagingInfo, String keyword) {
		int publisherId = publisherDao.getIdByUsername(username);
		return audioBookDao.getListAudioBookByUsername(publisherId, pagingInfo, keyword);
	}

}
